package com.wzz.util;

import java.sql.*;
import java.util.Properties;

public class DBUtil {
    private static String driver = null;
    private static String url = null;
    private static String user = null;
    private static String password = null;

    private static Properties ps = new Properties();

    static {
        try {
            ps.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            driver = ps.getProperty("mysql_driver");
            url = ps.getProperty("mysql_url");
            user = ps.getProperty("mysql_user");
            password = ps.getProperty("mysql_password");
            Class.forName(driver);

        } catch (Exception e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url,user,password);
    }

    public static void closeConnection(Connection con, Statement st, ResultSet rs)throws SQLException{
        if(rs != null){
            rs.close();
        }
        if (st != null){
            st.close();
        }
        if (con != null){
            con.close();
        }
    }

    public static void main(String[] args)throws SQLException{
        System.out.println(getConnection());
    }
}
